package com.hanhai.material.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.common.constant.StatusEnum;
import com.hanhai.material.common.result.KingdeeResult;
import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysProductInto;
import com.hanhai.material.device.entity.SysWorkPlan;
import com.hanhai.material.device.model.product.*;
import com.hanhai.material.device.service.SysProductIntoService;
import com.hanhai.material.device.service.SysWorkPlanService;
import com.hanhai.material.device.utils.kingdee.ProductReceiptUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;

import static com.hanhai.material.device.entity.table.SysBadRecordTableDef.SYS_BAD_RECORD;
import static com.hanhai.material.device.entity.table.SysProductIntoTableDef.SYS_PRODUCT_INTO;
import static com.hanhai.material.utils.TimeUtils.fromDateToString;
import static com.mybatisflex.core.query.QueryMethods.sum;

/**
 * 控制层。
 *
 * @author youchuang
 * @since 2024/04/13 08:30:32
 */
@RestController
@RequestMapping("/product_into")
@Log4j2
public class SysProductIntoController {

    @Autowired
    private SysProductIntoService sysProductIntoService;
    @Autowired
    private ProductReceiptUtil productReceiptUtil;

    @Autowired
    private SysWorkPlanService sysWorkPlanService;

    /**
     * 添加。
     *
     * @param sysProductInto
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public Result<?> save(@RequestBody SysProductInto sysProductInto) throws IOException {
        String dataId = UUID.randomUUID().toString();
        sysProductInto.setDataId(dataId);
        String productId = sysProductInto.getProductId();
        if (productId.startsWith("1.WC")) {
            sysProductInto.setStockId("X03.0");
        } else if (productId.startsWith("2.WC")) {
            sysProductInto.setStockId("X03.4");
        } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
            sysProductInto.setStockId("X01.0");
        } else if (productId.startsWith("2.YD117")) {
            sysProductInto.setStockId("X01.4");
        } else if (productId.startsWith("2.")) {
            sysProductInto.setStockId("X06.4");
        } else if (productId.startsWith("1.")) {
            sysProductInto.setStockId("X06.0");
        } else {
            sysProductInto.setStockId("X06.0");
        }
        sysProductInto.setStockPositionName("997");
        List<SysProductInto> productIntoList = new ArrayList<>();
        SysWorkPlan workPlan = this.sysWorkPlanService.getById(sysProductInto.getWorkPlanId());
        sysProductInto.setWorkPlan(workPlan);

        productIntoList.add(sysProductInto);
        Result<ProductReceiptData> result = this.getProductReceiptDataList(productIntoList, sysProductInto, 2);
        if (!result.getSuccess()) {
            return result;
        }

        boolean result1 = this.sysProductIntoService.save(sysProductInto);
        return result1 ? ResultTool.success(sysProductInto) : ResultTool.fail();
//        ProductReceiptData productReceiptData=result.getData();
//        KingdeeResult kingdeeResult= this.productReceiptUtil.add(productReceiptData);
//        if(kingdeeResult.getStatusCode()==200){
////            sysProductInto.setKingNo(kingdeeResult);
//            JSONObject jsonObject = JSONObject.parseObject(kingdeeResult.getData());
//            sysProductInto.setKingNo(jsonObject.getString("BillNo"));
//            sysProductInto.setKingId(jsonObject.getString("BillInterID"));
//            boolean result1 = this.sysProductIntoService.save(sysProductInto);
//            return result1 ? ResultTool.success(sysProductInto) : ResultTool.fail();
//        }else{
//            return ResultTool.fail(kingdeeResult.getMessage());
//        }


    }

    public Result getProductReceiptDataList(List<SysProductInto> productIntoList, SysProductInto productInto, Integer type) throws IOException {
        if (!productIntoList.isEmpty()) {

//            SysProductRecordInto productRecordInto = productInto.getProductRecordInto();
            SysWorkPlan workPlan = productInto.getWorkPlan();
            if (workPlan == null) {
                return ResultTool.fail("未查询到工单");
            }
//            log.info("ProductRecordInto:{}", JSONObject.toJSONString(productRecordInto));


            ProductReceiptData productReceiptData = new ProductReceiptData();
            Data data = new Data();

            /****************************Page1开始*******************************/
            Page1 page1 = new Page1();

            /*交货单位*/
            FDeptID fDeptID = new FDeptID();
            fDeptID.setFNumber(workPlan.getWorkShopId());
            fDeptID.setFName(workPlan.getWorkShop());
            page1.setFDeptID(fDeptID);

            /*红蓝单标志*/
            page1.setFROB("1");
            /*日期*/
            page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", productInto.getProductDate()));

            FDCStockID fdcStockID = new FDCStockID();
            FFManagerID ffManagerID = new FFManagerID();
            FSManagerID fsManagerID = new FSManagerID();
            switch (type) {
                case -2: {
                    fdcStockID.setFNumber("IN1.057");
                    fdcStockID.setFName("成型品质报废仓");
                    ffManagerID.setFName("HJ03900");
                    ffManagerID.setFNumber("HJ03900");
                    fsManagerID.setFName("HJ03900");
                    fsManagerID.setFNumber("HJ03900");
                    break;
                }
                case -1: {
                    fdcStockID.setFNumber("WF5.BF6");
                    fdcStockID.setFName("WF5.BF6");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");
                    fsManagerID.setFName("HJ00177");
                    fsManagerID.setFNumber("HJ00177");
                    break;
                }
                case 0: {
                    fdcStockID.setFNumber("WF5.BF5");
                    fdcStockID.setFName("WF5.BF5");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");

                    fsManagerID.setFName("HJ00177");
                    fsManagerID.setFNumber("HJ00177");

                    break;
                }
                case 1: {
                    fdcStockID.setFNumber("IN1.050");
                    fdcStockID.setFName("IN1.050");
                    ffManagerID.setFName("HJ03900");
                    ffManagerID.setFNumber("HJ03900");
                    fsManagerID.setFName("HJ03900");
                    fsManagerID.setFNumber("HJ03900");
                    break;

                }
                case 2: {
                    if (workPlan.getWorkPlanType().equals("普通订单")) {
                        String productId = productInto.getProductId();
                        if (productId.startsWith("1.WC")) {
                            fdcStockID.setFNumber("X03.0");
                            fdcStockID.setFName("X03.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.WC")) {

                            fdcStockID.setFNumber("X03.4");
                            fdcStockID.setFName("X03.4");

                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {
                            fdcStockID.setFNumber("X01.0");
                            fdcStockID.setFName("X01.0");
                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else if (productId.startsWith("2.YD117")) {

                            fdcStockID.setFNumber("X01.4");
                            fdcStockID.setFName("X01.4");


                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("2.")) {
                            fdcStockID.setFNumber("X06.4");
                            fdcStockID.setFName("X06.4");

                            ffManagerID.setFNumber("HJ00206");
                            ffManagerID.setFName("HJ00206");

                            fsManagerID.setFNumber("HJ00206");
                            fsManagerID.setFName("HJ00206");
                        } else if (productId.startsWith("1.")) {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        } else {

                            fdcStockID.setFNumber("X06.0");
                            fdcStockID.setFName("X06.0");

                            ffManagerID.setFNumber("HJ00312");
                            ffManagerID.setFName("HJ00312");

                            fsManagerID.setFNumber("HJ00312");
                            fsManagerID.setFName("HJ00312");
                        }
                    } else if (workPlan.getWorkPlanType().equals("试产")) {
                        fdcStockID.setFNumber("X03.7");
                        fdcStockID.setFName("X03.7");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    }
                    break;
                }

                default: {

                }
            }
            page1.setFFManagerID(ffManagerID);
            page1.setFSManagerID(fsManagerID);
            page1.setFDCStockID(fdcStockID);

            /*制单时间*/
            page1.setFHeadSelfA0232(fromDateToString("yyyy-MM-dd HH:mm:ss", new Date()));
            /*班别*/
            FHeadSelfA0233 fHeadSelfA0233 = new FHeadSelfA0233();
            fHeadSelfA0233.setFName(productInto.getShift());
            if (productInto.getShiftCode().equals("A班")) {
                fHeadSelfA0233.setFNumber("1");
            } else {
                fHeadSelfA0233.setFNumber("2");
            }

            page1.setFHeadSelfA0233(fHeadSelfA0233);

            /*源单类型*/
            page1.setFSelTranType("生产任务单");

            List<Page1> page1List = new ArrayList<>();
            page1List.add(page1);
            /****************************Page1结束*******************************/


            /****************************Page2开始*******************************/

            List<Page2> page2List = new ArrayList<>();
            int row = 0;
            for (SysProductInto sysProductInto : productIntoList) {
                log.error("Pages");
                SysWorkPlan workPlan1 = productInto.getWorkPlan();
                row++;
                Page2 page2 = new Page2();
                /*机台*/
                FEntrySelfA0244 fEntrySelfA0244 = new FEntrySelfA0244();
                fEntrySelfA0244.setFNumber(sysProductInto.getMachineName());
                fEntrySelfA0244.setFName(sysProductInto.getMachineName());
                page2.setFEntrySelfA0244(fEntrySelfA0244);
                /*行号*/
                page2.setFEntryID2(String.valueOf(row));
                /*物料编码*/
                FItemID fItemID = new FItemID();
                fItemID.setFNumber(sysProductInto.getProductId());
                fItemID.setFName("");
                page2.setFItemID(fItemID);

                /*物料名称*/
//            page2.setFItemName(productRecordInto.getProductName());

                /*单位*/
                FUnitID fUnitID = new FUnitID();
                fUnitID.setFNumber(workPlan1.getUnitId());
                fUnitID.setFName(workPlan1.getUnitName());
                page2.setFUnitID(fUnitID);

                /*生产日期*/
//            page2.setFKFDate(fromDateToString("yyyy-MM-dd", productRecordInto.getProductDate()));



                /*收货仓库*/
                FDCStockID1 fdcStockID1 = new FDCStockID1();
                FDCSPID fdcspid = new FDCSPID();
//                String desp = sysProductRecor.getGroupName() + sysProductInto.getShiftCode();

                switch (type) {
                    case -2: {
                        fdcStockID1.setFNumber("IN1.057");
                        fdcStockID1.setFName("成型品质报废仓");

                        QueryWrapper queryWrapper = QueryWrapper.create();
                        queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                        queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd", productInto.getProductDate())));
                        queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productInto.getShift()));
                        queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.eq("品质领取"));
                        queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                        queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                        List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                        StringJoiner stringJoiner = new StringJoiner(";");
                        int num = 0;
//                        for (SysBadRecord badRecord : badRecordList) {
//                            num += badRecord.getNum();
//                            if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                                stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//                            } else {
//                                stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                            }
//                        }
                        page2.setFauxqty(String.valueOf(num));
                        page2.setFQty(String.valueOf(num));

                        page2.setFEntrySelfA0247(workPlan1.getMachineName() + stringJoiner.toString());/**/
                        break;
                    }
                    case -1: {
                        fdcStockID1.setFNumber("WF5.BF6");
                        fdcStockID1.setFName("WF5.BF6");
                        fdcspid.setFName("921");
                        fdcspid.setFNumber("921");
                        page2.setFDCSPID(fdcspid);

                        QueryWrapper queryWrapper = QueryWrapper.create();
                        queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                        queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd", productInto.getProductDate())));

                        queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productInto.getShift()));
                        queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.likeLeft("薄膜"));
                        queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                        queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                        queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                        queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                        List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                        StringJoiner stringJoiner = new StringJoiner(";");
                        int num = 0;
//                        for (SysBadRecord badRecord : badRecordList) {
//                            num += badRecord.getNum();
//                            if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                                stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//
//                            } else {
//                                stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                            }
//                        }
                        //                   page2.setFAuxQtyMust(String.valueOf(num));
                        page2.setFauxqty(String.valueOf(num));
                        page2.setFQty(String.valueOf(num));

                        page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                        break;
                    }
                    case 0: {
                        fdcStockID1.setFNumber("WF5.BF5");
                        fdcStockID1.setFName("报废仓");
                        fdcspid.setFName("977");
                        fdcspid.setFNumber("977");
                        page2.setFDCSPID(fdcspid);

                        QueryWrapper queryWrapper = QueryWrapper.create();
                        queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan1.getWorkPlanId()));
                        queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd", productInto.getProductDate())));

                        queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productInto.getShift()));
                        queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLikeLeft("薄膜"));
                        queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
                        queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
                        queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
                        queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                        List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                        StringJoiner stringJoiner = new StringJoiner(";");
//                        int num = 0;
//                        for (SysBadRecord badRecord : badRecordList) {
//                            num += badRecord.getNum();
//                            if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                                stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//
//                            } else {
//                                stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                            }
//                        }
                        //                   page2.setFAuxQtyMust(String.valueOf(num));
//                        page2.setFauxqty(String.valueOf(num));
//                        page2.setFQty(String.valueOf(num));
                        page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                        break;
                    }
                    case 1: {
                        //                   page2.setFAuxQtyMust(String.valueOf(sysProductRecordInto.getDetentionNum() + sysProductRecordInto.getWillProcessedNum()));
//                        page2.setFauxqty(String.valueOf(productInto.getDetentionNum() + productInto.getWillProcessedNum()));
//                        page2.setFQty(String.valueOf(productInto.getDetentionNum() + productInto.getWillProcessedNum()));
                        fdcStockID1.setFNumber("IN1.050");
                        fdcStockID1.setFName("成型待入仓");
//                        fdcspid.setFName(desp);
//                        fdcspid.setFNumber(desp);
                        page2.setFDCSPID(fdcspid);

                        break;
                    }
                    case 2: {
                        //                   page2.setFAuxQtyMust(String.valueOf(sysProductRecordInto.getGoodNumPeople() + sysProductRecordInto.getLastTailCountInto()));
                        page2.setFAuxQtyMust(String.valueOf(productInto.getNum()));
                        page2.setFQtyMust(String.valueOf(productInto.getNum()));
                        fdcspid.setFName("977");
                        fdcspid.setFNumber("977");
                        page2.setFDCSPID(fdcspid);
                        if (workPlan1.getWorkPlanType().equals("普通订单")) {
                            String productId = sysProductInto.getProductId();
                            if (productId.startsWith("1.WC")) {
                                fdcStockID1.setFNumber("X03.0");
                                fdcStockID1.setFName("X03.0");

                            } else if (productId.startsWith("2.WC")) {

                                fdcStockID1.setFNumber("X03.4");
                                fdcStockID1.setFName("X03.4");

                            } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                                fdcStockID1.setFNumber("X01.0");
                                fdcStockID1.setFName("X01.0");

                            } else if (productId.startsWith("2.YD117")) {

                                fdcStockID1.setFNumber("X01.4");
                                fdcStockID1.setFName("X01.4");

                            } else if (productId.startsWith("2.")) {
                                fdcStockID1.setFNumber("X06.4");
                                fdcStockID1.setFName("X06.4");

                            } else if (productId.startsWith("1.")) {

                                fdcStockID1.setFNumber("X06.0");
                                fdcStockID1.setFName("X06.0");
                            } else {
                                fdcStockID1.setFNumber("X06.0");
                                fdcStockID1.setFName("X06.0");

                            }
                        } else if (workPlan.getWorkPlanType().equals("试产")) {
                            fdcStockID1.setFNumber("X03.7");
                            fdcStockID1.setFName("X03.7");
                        }
                        try {
                            page2.setFnote(productInto.getNote());
                        } catch (Exception e) {

                        }


                        break;
                    }
                    default: {

                    }
                }


                page2.setFDCStockID1(fdcStockID1);

                /*源单单号*/
                page2.setFSourceBillNo(workPlan1.getWorkPlanId());
                /*生产任务单号*/
                page2.setFICMOBillNo(workPlan1.getWorkPlanId());

                /*源单内码*/
                page2.setFSourceInterId(String.valueOf(workPlan1.getInterId()));
                /*源单类型*/
                page2.setFSourceTranType(String.valueOf(workPlan1.getTranTypeId()));
                /*任务单内码*/
                page2.setFICMOInterID(String.valueOf(workPlan1.getInterId()));
//                if (Integer.parseInt(page2.getFQty()) > 0) {
                page2List.add(page2);
//                }


//                    }
                /****************************Page2结束*******************************/


            }


            data.setPage1(page1List);
            data.setPage2(page2List);
            productReceiptData.setData(data);
            return ResultTool.success(productReceiptData);


        }
        return ResultTool.fail();
    }


    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public Result<?> remove(@PathVariable Integer id) throws IOException {
        SysProductInto productInto = this.sysProductIntoService.getById(id);
        if (productInto == null) {
            return ResultTool.fail("数据不存在");
        }
        if (StringUtils.isNotBlank(productInto.getKingNo())) {
            KingdeeResult kingdeeResult = this.productReceiptUtil.remove(productInto.getKingNo());
            if (kingdeeResult.getStatusCode() == 200) {
                boolean result = this.sysProductIntoService.removeById(id);
                return result ? ResultTool.success() : ResultTool.fail();
            } else {
                boolean result = this.sysProductIntoService.removeById(id);
                return result ? ResultTool.success() : ResultTool.fail();
            }
        } else {

            boolean result = this.sysProductIntoService.removeById(id);
            return result ? ResultTool.success() : ResultTool.fail();
        }

    }


    /**
     * 根据主键更新。
     *
     * @param sysProductInto
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    public Result<?> update(@RequestBody SysProductInto sysProductInto) throws IOException {
        SysProductInto productInto = this.sysProductIntoService.getById(sysProductInto.getId());
        if (productInto == null) {
            return ResultTool.fail("数据不存在");
        }
        SysWorkPlan workPlan = this.sysWorkPlanService.getById(sysProductInto.getWorkPlanId());
        sysProductInto.setWorkPlan(workPlan);
        if (StringUtils.isNotBlank(sysProductInto.getKingNo())) {
            UpdateProductReceiptData updateProductReceiptData = getUpdateProductReceipt(sysProductInto, 2);
            KingdeeResult kingdeeResult = this.productReceiptUtil.update(updateProductReceiptData);
            if (kingdeeResult.getStatusCode() == 200) {
                boolean result = this.sysProductIntoService.updateById(sysProductInto);
                return result ? ResultTool.success() : ResultTool.fail();
            } else {
                return ResultTool.fail(kingdeeResult.getMessage() + "：" + kingdeeResult.getData());
            }
        } else {

            boolean result = this.sysProductIntoService.updateById(sysProductInto);
            return result ? ResultTool.success() : ResultTool.fail();
        }
    }

    public UpdateProductReceiptData getUpdateProductReceipt(SysProductInto productInto, Integer type) throws IOException {
        if (productInto != null) {
            log.info("ProductRecordInto:{}", JSONObject.toJSONString(productInto));
            SysWorkPlan workPlan = productInto.getWorkPlan();

            UpdateProductReceiptData productReceiptData = new UpdateProductReceiptData();
            Data data = new Data();

            /****************************Page1开始*******************************/
            Page1 page1 = new Page1();

            /*交货单位*/
            FDeptID fDeptID = new FDeptID();
            fDeptID.setFNumber(workPlan.getWorkShopId());
            fDeptID.setFName(workPlan.getWorkShop());
            page1.setFDeptID(fDeptID);

            /*红蓝单标志*/
            page1.setFROB("1");
            /*日期*/
            page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", productInto.getProductDate()));
            FDCStockID fdcStockID = new FDCStockID();
            FFManagerID ffManagerID = new FFManagerID();
            FSManagerID fsManagerID = new FSManagerID();
            switch (type) {
                case -1: {
                    fdcStockID.setFNumber("WF5.BF6");
                    fdcStockID.setFName("WF5.BF6");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");
                    break;
                }
                case 0: {
                    fdcStockID.setFNumber("WF5.BF5");
                    fdcStockID.setFName("WF5.BF5");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");

                    break;
                }
                case 1: {
                    fdcStockID.setFNumber("IN1.050");
                    fdcStockID.setFName("IN1.050");
                    ffManagerID.setFName("HJ03900");
                    ffManagerID.setFNumber("HJ03900");
                    break;

                }
                case 2: {
                    String productId = productInto.getProductId();
                    if (productId.startsWith("1.WC")) {
                        fdcStockID.setFNumber("X03.0");
                        fdcStockID.setFName("X03.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.WC")) {

                        fdcStockID.setFNumber("X03.4");
                        fdcStockID.setFName("X03.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                        fdcStockID.setFNumber("X01.0");
                        fdcStockID.setFName("X01.0");


                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.YD117")) {

                        fdcStockID.setFNumber("X01.4");
                        fdcStockID.setFName("X01.4");


                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("2.")) {
                        fdcStockID.setFNumber("X06.4");
                        fdcStockID.setFName("X06.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.")) {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    }
                    page1.setFFManagerID(ffManagerID);
                    page1.setFSManagerID(fsManagerID);
                    page1.setFDCStockID(fdcStockID);
                    break;
                }
                default: {

                }
            }

            /*制单时间*/
            page1.setFHeadSelfA0232(fromDateToString("yyyy-MM-dd HH:mm:ss", new Date()));
            /*班别*/
            FHeadSelfA0233 fHeadSelfA0233 = new FHeadSelfA0233();
            fHeadSelfA0233.setFName(productInto.getShift());
            if (productInto.getShiftCode().equals("A班")) {
                fHeadSelfA0233.setFNumber("1");
            } else {
                fHeadSelfA0233.setFNumber("2");
            }

            page1.setFHeadSelfA0233(fHeadSelfA0233);

            /*源单类型*/
            page1.setFSelTranType("生产任务单");

            List<Page1> page1List = new ArrayList<>();
            page1List.add(page1);
            /****************************Page1结束*******************************/


            /****************************Page2开始*******************************/
            Page2 page2 = new Page2();
            /*机台*/
            FEntrySelfA0244 fEntrySelfA0244 = new FEntrySelfA0244();
            fEntrySelfA0244.setFNumber(productInto.getMachineName());
            fEntrySelfA0244.setFName(productInto.getMachineName());
            page2.setFEntrySelfA0244(fEntrySelfA0244);

            /*行号*/
            page2.setFEntryID2("1");
            /*物料编码*/
            FItemID fItemID = new FItemID();
            fItemID.setFNumber(productInto.getProductId());
            fItemID.setFName("");
            page2.setFItemID(fItemID);

            /*物料名称*/
//            page2.setFItemName(productRecordInto.getProductName());

            /*单位*/
            FUnitID fUnitID = new FUnitID();
            fUnitID.setFNumber(workPlan.getUnitId());
            fUnitID.setFName(workPlan.getUnitName());
            page2.setFUnitID(fUnitID);

            /*生产日期*/
//            page2.setFKFDate(fromDateToString("yyyy-MM-dd", productRecordInto.getProductDate()));

            /*仓位*/
//            FDCSPID fdcspid=new FDCSPID();
//            fdcspid.setFName();
//            page2.setFDCSPID();

            /*收货仓库*/
            FDCStockID1 fdcStockID1 = new FDCStockID1();
            FDCSPID fdcspid = new FDCSPID();
//            String desp=productRecordInto.getGroupName()+productRecordInto.getShiftCode();

            switch (type) {
                case -1: {
                    fdcStockID1.setFNumber("WF5.BF5");
                    fdcStockID1.setFName("报废仓");
                    fdcspid.setFName("987");
                    fdcspid.setFNumber("987");
                    page2.setFDCSPID(fdcspid);

//                    QueryWrapper queryWrapper = QueryWrapper.create();
//                    queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
//                    queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productInto.getProductDate()));
//                    queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productInto.getShift()));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLikeLeft("薄膜"));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
//                    queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                    queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                    List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                    StringJoiner stringJoiner = new StringJoiner(";");
                    int num = 0;
//                    for (SysBadRecord badRecord : badRecordList) {
//                        num += badRecord.getNum();
//                        if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                            stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//
//                        } else {
//                            stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                        }
//                    }
// //                   page2.setFAuxQtyMust(String.valueOf(num));
                    page2.setFauxqty(String.valueOf(num));
                    page2.setFQty(String.valueOf(num));
                    page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                    break;
                }
                case 0: {
                    fdcStockID1.setFNumber("WF5.BF5");
                    fdcStockID1.setFName("报废仓");
                    fdcspid.setFName("987");
                    fdcspid.setFNumber("987");
                    page2.setFDCSPID(fdcspid);

//                    QueryWrapper queryWrapper = QueryWrapper.create();
//                    queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
//                    queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productRecordInto.getProductDate()));
//                    queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLikeLeft("薄膜"));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
//                    queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                    queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                    List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                    StringJoiner stringJoiner = new StringJoiner(";");
                    int num = 0;
//                    for (SysBadRecord badRecord : badRecordList) {
//                        num += badRecord.getNum();
//                        if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                            stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//
//                        } else {
//                            stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                        }
//                    }
                    //                   page2.setFAuxQtyMust(String.valueOf(num));
                    page2.setFauxqty(String.valueOf(num));
                    page2.setFQty(String.valueOf(num));
                    page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                    break;
                }
                case 1: {
                    //                   page2.setFAuxQtyMust(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
//                    page2.setFauxqty(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
//                    page2.setFQty(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
//                    fdcStockID1.setFNumber("IN1.050");
//                    fdcStockID1.setFName("成型待入仓");
//                    fdcspid.setFName(desp);
//                    fdcspid.setFNumber(desp);
//                    page2.setFDCSPID(fdcspid);
                    break;
                }
                case 2: {
                    //                   page2.setFAuxQtyMust(String.valueOf(productRecordInto.getGoodNumPeople() + productRecordInto.getLastTailCountInto()));
                    page2.setFAuxQtyMust(String.valueOf(productInto.getNum()));
                    page2.setFQtyMust(String.valueOf(productInto.getNum()));

                    fdcspid.setFName("977");
                    fdcspid.setFNumber("977");
                    page2.setFDCSPID(fdcspid);

                    String productId = productInto.getProductId();
                    if (productId.startsWith("1.WC")) {
                        fdcStockID1.setFNumber("X03.0");
                        fdcStockID1.setFName("X03.0");

                    } else if (productId.startsWith("2.WC")) {

                        fdcStockID1.setFNumber("X03.4");
                        fdcStockID1.setFName("X03.4");

                    } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                        fdcStockID1.setFNumber("X01.0");
                        fdcStockID1.setFName("X01.0");

                    } else if (productId.startsWith("2.YD117")) {

                        fdcStockID1.setFNumber("X01.4");
                        fdcStockID1.setFName("X01.4");

                    } else if (productId.startsWith("2.")) {
                        fdcStockID1.setFNumber("X06.4");
                        fdcStockID1.setFName("X06.4");

                    } else if (productId.startsWith("1.")) {

                        fdcStockID1.setFNumber("X06.0");
                        fdcStockID1.setFName("X06.0");
                    } else {
                        fdcStockID1.setFNumber("X06.0");
                        fdcStockID1.setFName("X06.0");

                    }

                    break;
                }
                default: {

                }
            }


            page2.setFDCStockID1(fdcStockID1);
            /*源单单号*/
            page2.setFSourceBillNo(workPlan.getWorkPlanId());
            /*生产任务单号*/
            page2.setFICMOBillNo(workPlan.getWorkPlanId());

            /*源单内码*/
            page2.setFSourceInterId(String.valueOf(workPlan.getInterId()));
            /*源单类型*/
            page2.setFSourceTranType(String.valueOf(workPlan.getTranTypeId()));
            /*任务单内码*/
            page2.setFICMOInterID(String.valueOf(workPlan.getInterId()));

            List<Page2> page2List = new ArrayList<>();
            page2List.add(page2);
            /****************************Page2结束*******************************/

            data.setPage1(page1List);
            data.setPage2(page2List);
            productReceiptData.setData(data);

            switch (type) {
                case -1: {
//                    productReceiptData.setFBillNo(productRecordInto.getKingBadBmNo());
                    break;
                }
                case 0: {
//                    productReceiptData.setFBillNo(productRecordInto.getKingBadNo());
                    break;
                }
                case 1: {
//                    productReceiptData.setFBillNo(productRecordInto.getKingDetentionNo());
                    break;
                }
                case 2: {
                    productReceiptData.setFBillNo(productInto.getKingNo());
                    break;
                }
                default: {

                }
            }

            log.error("Data:{}", JSONObject.toJSONString(productReceiptData));
            return productReceiptData;
        }
        return null;
    }

    public UpdateProductReceiptData getConfirmUpdateProductReceipt(SysProductInto productInto, Integer type) throws IOException {
        if (productInto != null) {
            log.info("ProductRecordInto:{}", JSONObject.toJSONString(productInto));
            SysWorkPlan workPlan = productInto.getWorkPlan();

            UpdateProductReceiptData productReceiptData = new UpdateProductReceiptData();
            Data data = new Data();

            /****************************Page1开始*******************************/
            Page1 page1 = new Page1();

            /*交货单位*/
            FDeptID fDeptID = new FDeptID();
            fDeptID.setFNumber(workPlan.getWorkShopId());
            fDeptID.setFName(workPlan.getWorkShop());
            page1.setFDeptID(fDeptID);

            /*红蓝单标志*/
            page1.setFROB("1");
            /*日期*/
            page1.setFdate(fromDateToString("yyyy-MM-dd 00:00:00", productInto.getProductDate()));
            FDCStockID fdcStockID = new FDCStockID();
            FFManagerID ffManagerID = new FFManagerID();
            FSManagerID fsManagerID = new FSManagerID();
            switch (type) {
                case -1: {
                    fdcStockID.setFNumber("WF5.BF6");
                    fdcStockID.setFName("WF5.BF6");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");
                    break;
                }
                case 0: {
                    fdcStockID.setFNumber("WF5.BF5");
                    fdcStockID.setFName("WF5.BF5");
                    ffManagerID.setFName("HJ00177");
                    ffManagerID.setFNumber("HJ00177");

                    break;
                }
                case 1: {
                    fdcStockID.setFNumber("IN1.050");
                    fdcStockID.setFName("IN1.050");
                    ffManagerID.setFName("HJ03900");
                    ffManagerID.setFNumber("HJ03900");
                    break;

                }
                case 2: {
                    String productId = productInto.getProductId();
                    if (productId.startsWith("1.WC")) {
                        fdcStockID.setFNumber("X03.0");
                        fdcStockID.setFName("X03.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.WC")) {

                        fdcStockID.setFNumber("X03.4");
                        fdcStockID.setFName("X03.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                        fdcStockID.setFNumber("X01.0");
                        fdcStockID.setFName("X01.0");


                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else if (productId.startsWith("2.YD117")) {

                        fdcStockID.setFNumber("X01.4");
                        fdcStockID.setFName("X01.4");


                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("2.")) {
                        fdcStockID.setFNumber("X06.4");
                        fdcStockID.setFName("X06.4");

                        ffManagerID.setFNumber("HJ00206");
                        ffManagerID.setFName("HJ00206");

                        fsManagerID.setFNumber("HJ00206");
                        fsManagerID.setFName("HJ00206");
                    } else if (productId.startsWith("1.")) {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    } else {

                        fdcStockID.setFNumber("X06.0");
                        fdcStockID.setFName("X06.0");

                        ffManagerID.setFNumber("HJ00312");
                        ffManagerID.setFName("HJ00312");

                        fsManagerID.setFNumber("HJ00312");
                        fsManagerID.setFName("HJ00312");
                    }
                    page1.setFFManagerID(ffManagerID);
                    page1.setFSManagerID(fsManagerID);
                    page1.setFDCStockID(fdcStockID);
                    break;
                }
                default: {

                }
            }

            /*制单时间*/
            page1.setFHeadSelfA0232(fromDateToString("yyyy-MM-dd HH:mm:ss", new Date()));
            /*班别*/
            FHeadSelfA0233 fHeadSelfA0233 = new FHeadSelfA0233();
            fHeadSelfA0233.setFName(productInto.getShift());
            if (productInto.getShiftCode().equals("A班")) {
                fHeadSelfA0233.setFNumber("1");
            } else {
                fHeadSelfA0233.setFNumber("2");
            }

            page1.setFHeadSelfA0233(fHeadSelfA0233);

            /*源单类型*/
            page1.setFSelTranType("生产任务单");

            List<Page1> page1List = new ArrayList<>();
            page1List.add(page1);
            /****************************Page1结束*******************************/


            /****************************Page2开始*******************************/
            Page2 page2 = new Page2();
            /*机台*/
            FEntrySelfA0244 fEntrySelfA0244 = new FEntrySelfA0244();
            fEntrySelfA0244.setFNumber(productInto.getMachineName());
            fEntrySelfA0244.setFName(productInto.getMachineName());
            page2.setFEntrySelfA0244(fEntrySelfA0244);

            /*行号*/
            page2.setFEntryID2("1");
            /*物料编码*/
            FItemID fItemID = new FItemID();
            fItemID.setFNumber(productInto.getProductId());
            fItemID.setFName("");
            page2.setFItemID(fItemID);

            /*物料名称*/
//            page2.setFItemName(productRecordInto.getProductName());

            /*单位*/
            FUnitID fUnitID = new FUnitID();
            fUnitID.setFNumber(workPlan.getUnitId());
            fUnitID.setFName(workPlan.getUnitName());
            page2.setFUnitID(fUnitID);

            /*生产日期*/
//            page2.setFKFDate(fromDateToString("yyyy-MM-dd", productRecordInto.getProductDate()));

            /*仓位*/
//            FDCSPID fdcspid=new FDCSPID();
//            fdcspid.setFName();
//            page2.setFDCSPID();

            /*收货仓库*/
            FDCStockID1 fdcStockID1 = new FDCStockID1();
            FDCSPID fdcspid = new FDCSPID();
//            String desp=productRecordInto.getGroupName()+productRecordInto.getShiftCode();

            switch (type) {
                case -1: {
                    fdcStockID1.setFNumber("WF5.BF5");
                    fdcStockID1.setFName("报废仓");
                    fdcspid.setFName("987");
                    fdcspid.setFNumber("987");
                    page2.setFDCSPID(fdcspid);

//                    QueryWrapper queryWrapper = QueryWrapper.create();
//                    queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
//                    queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productInto.getProductDate()));
//                    queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productInto.getShift()));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLikeLeft("薄膜"));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
//                    queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                    queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                    List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                    StringJoiner stringJoiner = new StringJoiner(";");
                    int num = 0;
//                    for (SysBadRecord badRecord : badRecordList) {
//                        num += badRecord.getNum();
//                        if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                            stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//
//                        } else {
//                            stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                        }
//                    }
// //                   page2.setFAuxQtyMust(String.valueOf(num));
                    page2.setFauxqty(String.valueOf(num));
                    page2.setFQty(String.valueOf(num));
                    page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                    break;
                }
                case 0: {
                    fdcStockID1.setFNumber("WF5.BF5");
                    fdcStockID1.setFName("报废仓");
                    fdcspid.setFName("987");
                    fdcspid.setFNumber("987");
                    page2.setFDCSPID(fdcspid);

//                    QueryWrapper queryWrapper = QueryWrapper.create();
//                    queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(workPlan.getWorkPlanId()));
//                    queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(productRecordInto.getProductDate()));
//                    queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(productRecordInto.getShift()));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.notLikeLeft("薄膜"));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.ne("品质领取"));
//                    queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.ne("调机空模"));
//                    queryWrapper.select(SYS_BAD_RECORD.MACHINE_NAME, SYS_BAD_RECORD.PRODUCT_ID, sum(SYS_BAD_RECORD.NUM).as("num"), SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                    queryWrapper.groupBy(SYS_BAD_RECORD.REASON_TYPE, SYS_BAD_RECORD.REASON_NAME);
//                    List<SysBadRecord> badRecordList = this.sysBadRecordService.list(queryWrapper);
                    StringJoiner stringJoiner = new StringJoiner(";");
                    int num = 0;
//                    for (SysBadRecord badRecord : badRecordList) {
//                        num += badRecord.getNum();
//                        if (badRecord.getReasonType().equals(badRecord.getReasonName())) {
//                            stringJoiner.add(badRecord.getReasonName() + badRecord.getNum() + "PCS");
//
//                        } else {
//                            stringJoiner.add(badRecord.getReasonType() + "(" + badRecord.getReasonName() + ")" + badRecord.getNum() + "PCS");
//                        }
//                    }
                    //                   page2.setFAuxQtyMust(String.valueOf(num));
                    page2.setFauxqty(String.valueOf(num));
                    page2.setFQty(String.valueOf(num));
                    page2.setFEntrySelfA0247(stringJoiner.toString());/**/
                    break;
                }
                case 1: {
                    //                   page2.setFAuxQtyMust(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
//                    page2.setFauxqty(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
//                    page2.setFQty(String.valueOf(productRecordInto.getDetentionNum() + productRecordInto.getWillProcessedNum()));
//                    fdcStockID1.setFNumber("IN1.050");
//                    fdcStockID1.setFName("成型待入仓");
//                    fdcspid.setFName(desp);
//                    fdcspid.setFNumber(desp);
//                    page2.setFDCSPID(fdcspid);
                    break;
                }
                case 2: {
                    //                   page2.setFAuxQtyMust(String.valueOf(productRecordInto.getGoodNumPeople() + productRecordInto.getLastTailCountInto()));
                    page2.setFauxqty(String.valueOf(productInto.getActualNum()));
                    page2.setFQty(String.valueOf(productInto.getActualNum()));
                    page2.setFAuxQtyMust(String.valueOf(productInto.getNum()));
                    page2.setFQtyMust(String.valueOf(productInto.getNum()));
                    fdcspid.setFName("977");
                    fdcspid.setFNumber("977");
                    page2.setFDCSPID(fdcspid);

                    String productId = productInto.getProductId();
                    if (productId.startsWith("1.WC")) {
                        fdcStockID1.setFNumber("X03.0");
                        fdcStockID1.setFName("X03.0");

                    } else if (productId.startsWith("2.WC")) {

                        fdcStockID1.setFNumber("X03.4");
                        fdcStockID1.setFName("X03.4");

                    } else if (productId.startsWith("1.YD117") || productId.startsWith("1.YD1A") || productId.startsWith("1.YD1M")) {

                        fdcStockID1.setFNumber("X01.0");
                        fdcStockID1.setFName("X01.0");

                    } else if (productId.startsWith("2.YD117")) {

                        fdcStockID1.setFNumber("X01.4");
                        fdcStockID1.setFName("X01.4");

                    } else if (productId.startsWith("2.")) {
                        fdcStockID1.setFNumber("X06.4");
                        fdcStockID1.setFName("X06.4");

                    } else if (productId.startsWith("1.")) {

                        fdcStockID1.setFNumber("X06.0");
                        fdcStockID1.setFName("X06.0");
                    } else {
                        fdcStockID1.setFNumber("X06.0");
                        fdcStockID1.setFName("X06.0");

                    }

                    break;
                }
                default: {

                }
            }


            page2.setFDCStockID1(fdcStockID1);
            /*源单单号*/
            page2.setFSourceBillNo(workPlan.getWorkPlanId());
            /*生产任务单号*/
            page2.setFICMOBillNo(workPlan.getWorkPlanId());

            /*源单内码*/
            page2.setFSourceInterId(String.valueOf(workPlan.getInterId()));
            /*源单类型*/
            page2.setFSourceTranType(String.valueOf(workPlan.getTranTypeId()));
            /*任务单内码*/
            page2.setFICMOInterID(String.valueOf(workPlan.getInterId()));

            List<Page2> page2List = new ArrayList<>();
            page2List.add(page2);
            /****************************Page2结束*******************************/

            data.setPage1(page1List);
            data.setPage2(page2List);
            productReceiptData.setData(data);

            switch (type) {
                case -1: {
//                    productReceiptData.setFBillNo(productRecordInto.getKingBadBmNo());
                    break;
                }
                case 0: {
//                    productReceiptData.setFBillNo(productRecordInto.getKingBadNo());
                    break;
                }
                case 1: {
//                    productReceiptData.setFBillNo(productRecordInto.getKingDetentionNo());
                    break;
                }
                case 2: {
                    productReceiptData.setFBillNo(productInto.getKingNo());
                    break;
                }
                default: {

                }
            }

            log.error("Data:{}", JSONObject.toJSONString(productReceiptData));
            return productReceiptData;
        }
        return null;
    }


    @PostMapping("reject")
    public Result<?> reject(@RequestBody SysProductInto sysProductInto) throws IOException {
        SysProductInto productInto = this.sysProductIntoService.getById(sysProductInto.getId());
        if (productInto == null) {
            return ResultTool.fail("数据不存在");
        }
        sysProductInto.setCheckTime(new Date());
        if (StringUtils.isEmpty(sysProductInto.getKingNo())) {
            sysProductInto.setStatus(StatusEnum.REJECT_STATUS.getCode());
            boolean result = this.sysProductIntoService.updateById(sysProductInto);
            return result ? ResultTool.success() : ResultTool.fail();
        } else {
//            KingdeeResult kingdeeResult=this.productReceiptUtil.remove(sysProductInto.getKingNo());
//            if(kingdeeResult.getStatusCode()==200){
//                sysProductInto.setStatus(StatusEnum.REJECT_STATUS.getCode());
//                sysProductInto.setKingId(null);
//                sysProductInto.setKingNo(null);
//                boolean result = this.sysProductIntoService.updateById(sysProductInto,false);
//                return result ? ResultTool.success() : ResultTool.fail();
//            }else{
//                sysProductInto.setKingId(null);
//                sysProductInto.setKingNo(null);
//                sysProductInto.setStatus(StatusEnum.REJECT_STATUS.getCode());
//                boolean result = this.sysProductIntoService.updateById(sysProductInto,false);
//                return ResultTool.success();
//            }
            return ResultTool.success();
        }

    }

    @PostMapping("receive")
    public Result<?> receive(@RequestBody SysProductInto sysProductInto) throws IOException {
        SysProductInto productInto = this.sysProductIntoService.getById(sysProductInto.getId());
        if (productInto == null) {
            return ResultTool.fail("数据不存在");
        }
        SysWorkPlan workPlan = this.sysWorkPlanService.getById(sysProductInto.getWorkPlanId());
        sysProductInto.setWorkPlan(workPlan);
        if (StringUtils.isNotBlank(sysProductInto.getKingNo())) {
//            UpdateProductReceiptData updateProductReceiptData=getConfirmUpdateProductReceipt(sysProductInto,2);
//            log.error("J:{}",JSONObject.toJSONString(updateProductReceiptData));
//            KingdeeResult kingdeeResult=this.productReceiptUtil.update(updateProductReceiptData);
//
//            if(kingdeeResult.getStatusCode()==200){
//                boolean result = this.sysProductIntoService.updateById(sysProductInto);
//                return result ? ResultTool.success() : ResultTool.fail();
//            }else{
//                return ResultTool.fail(kingdeeResult.getMessage()+"："+kingdeeResult.getData());
//            }
            return ResultTool.success();

        } else {

            boolean result = this.sysProductIntoService.updateById(sysProductInto);
            return result ? ResultTool.success() : ResultTool.fail();
        }
    }

    @PostMapping("check")
    public Result<?> check(@RequestBody SysProductInto sysProductInto) throws IOException {
        sysProductInto.setCheckTime(new Date());
//        KingdeeResult kingdeeResult=this.productReceiptUtil.check(sysProductInto);
//        if(kingdeeResult.getStatusCode()!=200){
//            return ResultTool.fail(kingdeeResult.getMessage()+"："+kingdeeResult.getData());
//        }
        sysProductInto.setStatus(StatusEnum.OK_STATUS.getCode());
        boolean result = this.sysProductIntoService.updateById(sysProductInto);
        return result ? ResultTool.success() : ResultTool.fail();
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @PostMapping("list/{totalRows}/{page}/{size}")
    public Result<?> list(@RequestBody SysProductInto sysProductInto, @PathVariable long totalRows, @PathVariable long page, @PathVariable long size) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_INTO.SHIFT.eq(sysProductInto.getShift()));
        if (sysProductInto.getProductId() != null) {
            queryWrapper.where(SYS_PRODUCT_INTO.PRODUCT_ID.like(sysProductInto.getProductId()));
        }
        if (sysProductInto.getStartDate() != null && sysProductInto.getEndDate() != null) {
            queryWrapper.where(SYS_PRODUCT_INTO.PRODUCT_DATE.between(sysProductInto.getStartDate(), sysProductInto.getEndDate()));
        } else {
            if (sysProductInto.getEndDate() == null) {
                queryWrapper.where(SYS_PRODUCT_INTO.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd", sysProductInto.getProductDate())));

            }
        }
        if (sysProductInto.getMachineName() != null) {
            queryWrapper.where(SYS_PRODUCT_INTO.MACHINE_NAME.eq(sysProductInto.getMachineName()));
        }

        if (sysProductInto.getShift() != null) {
            queryWrapper.where(SYS_PRODUCT_INTO.SHIFT.eq(sysProductInto.getShift()));
        }
        if (sysProductInto.getShiftCode() != null) {
            queryWrapper.where(SYS_PRODUCT_INTO.SHIFT_CODE.eq(sysProductInto.getShiftCode()));
        }

        queryWrapper.orderBy(SYS_PRODUCT_INTO.CREATE_TIME, false);
        Page<SysProductInto> productIntoPage = new Page<>();

        productIntoPage.setPageNumber(page);
        productIntoPage.setPageSize(size);

        Page<SysProductInto> intoPage = this.sysProductIntoService.page(productIntoPage, queryWrapper);
        log.error("SQL:{}", queryWrapper.toSQL());
        return intoPage.getRecords().isEmpty() ? ResultTool.fail() : ResultTool.success(intoPage);
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public SysProductInto getInfo(@PathVariable Serializable id) {
        return sysProductIntoService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<SysProductInto> page(Page<SysProductInto> page) {
        return sysProductIntoService.page(page);
    }

}
